package com.itheima.publisher.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;

/**
 * ReturnCallback 回调配置:
 *     这段代码配置了RabbitMQ的消息返回回调。通过 MqConfig类的init方法，设置了一个回调函数，
 * 当消息无法路由到任何队列时触发。该回调会记录错误日志，并打印出交换机、路由键、消息内容、回
 * 复码和回复文本等信息。
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class MqConfig {

    private final RabbitTemplate rabbitTemplate;

    public void init() {
        rabbitTemplate.setReturnsCallback(returned -> {
            log.error("监听到了消息 return callback");
            log.debug("exchange：{}", returned.getExchange());
            log.debug("routingKey：{}", returned.getRoutingKey());
            log.debug("message：{}", returned.getMessage());
            log.debug("replyCode：{}", returned.getReplyCode());
            log.debug("replyText：{}", returned.getReplyText());

        });
    }

}
